package com.example.home.findWork.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.findWork.activity.WordDetailActivity;
import com.example.home.findWork.bean.WordHotBean;

import java.util.List;

public class WordAllAdapter extends RecyclerView.Adapter<WordAllAdapter.MyHoldy> {
    private Context context;
    private List<WordHotBean.RowsBean> list;

    public WordAllAdapter(Context context, List<WordHotBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    public void updateData(List<WordHotBean.RowsBean> newData) {
        this.list = newData;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public WordAllAdapter.MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_work, null);
        return new WordAllAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull WordAllAdapter.MyHoldy holder, int position) {
        holder.name.setText(list.get(position).getName());
        holder.salary.setText(list.get(position).getSalary());
        holder.address.setText(list.get(position).getAddress());
        holder.need.setText(list.get(position).getNeed());
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, WordDetailActivity.class);
                intent.putExtra("wordId",String.valueOf(list.get(position).getId()));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private TextView name;
        private TextView salary;
        private TextView address;
        private TextView need;
        private RelativeLayout rootView;

        public MyHoldy(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            salary = itemView.findViewById(R.id.salary);
            address = itemView.findViewById(R.id.address);
            need = itemView.findViewById(R.id.need);
            rootView = itemView.findViewById(R.id.rootView);
        }
    }
}
